package Env;
import java.util.Arrays;
import java.util.Vector;


public class Graph {
	private Edge[][] graph;
	public final int n;

	private class Edge {
	    private int cost;
	    private boolean flooded;


	    public Edge(int c,boolean flood){
	        cost=c;
	        flooded=flood;
	    }

	    public int getCost(){
	        return cost;
	    }

	    public boolean isFlooded(){
	        return flooded;
	    }

		@Override
		public String toString() {
			return "Edge [ "+ cost + ", " + flooded + "]";
		}
	}

	/*
	 * Construct a new Graph of size nxn
	 * pre: n>0
	 * post: Graph has no edges
	 */
	Graph(int n){
		this.n=n;
		graph = new Edge[n+1][n+1];
	}

	/*
	 * adds new edge to the graph
	 */
	void addEdge(int v1, int v2, int cost, boolean flooded) {
		Edge edge = this.new Edge(cost, flooded);
		graph[v1][v2] = edge;
		graph[v2][v1] = edge;
	}

	/*
	 * removes edge between two vertexes if existed
	 */
	void removeEdge(int v1, int v2) {
		graph[v1][v2] = graph[v2][v1] = null;
	}



	public boolean connected(int v1, int v2) {
		return graph[v1][v2] != null;
	}

	public int getEdgeCost(int v1, int v2) throws Exception {
		if (!connected(v1, v2))
			throw new Exception("There's no edge between "+ v1 + " and "+ v2 + "!");

		return graph[v1][v2].getCost();
	}

	public boolean isEdgeFlooded(int v1, int v2) throws Exception {
		if (!connected(v1, v2))
			throw new Exception("There's no edge between "+ v1 + " and "+ v2 + "!");

		return graph[v1][v2].isFlooded();
	}

	@Override
	public String toString() {
		return "Graph \n" + Arrays.toString(graph) + "\n";
	}
	
	
	public Vector<Integer> getAllConnected(int v){
		Vector<Integer> ans = new Vector<Integer>();
		for (int i=0;i<n;i++){
			if (graph[v][i]!=null) ans.add(i);
		}
		return ans;
	}
}
